package com.lgkj.ui.manager;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lgkj.R;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.PopupWindowHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/6 0006.
 */

public class ShopTrendsAddActivity extends BaseActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_sex)
    EditText etSex;
    @BindView(R.id.et_text)
    EditText etText;
    private String[] tagColors = {"f33d60", "117ee5", "ec4d1f", "3fb32c"};
    private String tagColor = "f33d60";
    private PopupWindowHelper popupWindowHelper;
    private View popView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_trends_add;
    }

    @Override
    public void initView() {
        setTitle("上传店铺动态");
    }

    @Override
    public void initData() {
        popView = LayoutInflater.from(this).inflate(R.layout.popupview, null);
        popupWindowHelper = new PopupWindowHelper(popView);
        popView.findViewById(R.id.tv_item1).setOnClickListener(clickItem);
        popView.findViewById(R.id.tv_item2).setOnClickListener(clickItem);
        popView.findViewById(R.id.tv_item3).setOnClickListener(clickItem);
        popView.findViewById(R.id.tv_item4).setOnClickListener(clickItem);
    }


    @OnClick({R.id.ibtn_chooice, R.id.ibtn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_chooice:
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(getCurrentFocus()
                                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                popupWindowHelper.showAsDropDown(view);
                break;
            case R.id.ibtn_save:
                save();
                break;
        }
    }

    /**
     * http://mda.lianguangtech.com/api/shop_Dynamic/shopDynamicAdd?sid=35d74b9faa1550c010484802469748b1&tag=节日快乐&title=测试&content=pls哦rosy所以嘻嘻嘻嘻嘻嘻嘻&access_token=80a0d224500097b389590ce3d2c2ce17&tag_color=#f33d60
     */
    private void save() {
        showDiaLog("正在添加");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopTrendsAddActivity.this));
        map.put("shop_id", CommonUtils.getShopId(ShopTrendsAddActivity.this));
        map.put("tag", etName.getText().toString());
        map.put("title", etSex.getText().toString());
        map.put("content", etText.getText().toString());
        map.put("tag_color", "#" + tagColor);
        HttpUtils.postHttp(map, CommonConstant.shopDynamicAdd, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    ShopTrendsActivity.isRefresh=true;
                    finish();
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });

    }

    private View.OnClickListener clickItem = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_item1:
                    tagColor = tagColors[0];
                    break;
                case R.id.tv_item2:
                    tagColor = tagColors[1];
                    break;
                case R.id.tv_item3:
                    tagColor = tagColors[2];
                    break;
                case R.id.tv_item4:
                    tagColor = tagColors[3];
                    break;
            }
            etName.setTextColor(Color.parseColor("#" + tagColor));
            popupWindowHelper.dismiss();
        }
    };
}
